package com.it.myh;

import java.io.*;

/**
 * 实现非文本文件的复制
 */
public class Od_0215_02 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        //1.造文件
        File file = new File("C:\\Users\\Lenovo\\Pictures\\bbb0a490495207859a1837b09bd3c5d.jpg");
        File picture = new File("D:\\sssmmmqqq.jpg");
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            //2.造流
            //2.1 造节点流
            FileInputStream fileInputStream = new FileInputStream(file);
            FileOutputStream fileOutputStream = new FileOutputStream(picture);
            //2.2 造处理流--缓冲流
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            //3.复制过程：先读后写
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bufferedInputStream.read(bytes)) != -1){
                bufferedOutputStream.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关流：在关闭外层流的同时，自动给我们关闭了里层的流。
            if (bufferedInputStream != null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedOutputStream != null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
